<!-- 搜索项目 -->
<template>
  <custom-select-input-popup ref="hSelectInputPopup" v-model="val" v-bind="$attrs" v-on="$listeners" :params="queryParams"
    :keywords.sync="keywords" :getList="getList" :useSearch="false" :isUnique="false" :records="false" :isMulti="muitFlag"
    @getDetail="getDetail" @change="$emit('change', $event)">
    <template v-slot:default="{ item }">
      <view class="project-item">
        <view class="option">
          <view class="lf">类别名称</view>
          <view class="rl">{{ item.categoryName }}</view>
        </view>
        <view class="option">
          <view class="lf">类别编码</view>
          <view class="rl">{{ item.categoryCode }}</view>
        </view>
      </view>
    </template>
  </custom-select-input-popup>
</template>

<script>
import { erpPurchaseCategoryChildList } from '@/api/index.js'
// import { filterMultiDictText } from '@/utils/JDictSelectUtil.js'

export default {
  name: 'HSelectExemptionObjectsInputPopup',
  data() {
    return {
      getList: erpPurchaseCategoryChildList,
      params: {
        // column: 'createTime',
        // order: 'desc',
        pageNo: 1,
        // pageSize: 10,
        // docBigCategory: '',
        // docSmallCategory: '',
        // field: 'id,,,billNo,docBigCategory_dictText,docSmallCategory_dictText,teamId_dictText,classification_dictText,voucherNumberRange,totalNumber,docAddress',
      },
      keywords: '',
    }
  },
  // model: {
  //   prop: 'value',
  //   event: 'input',
  // },
  props: {
    value: {
      type: [String, Number, Array],
      default: null,
    },
    muitFlag: {
      type: Boolean,
      default: false,
    },
    parentId: {
      type: String,
      default: '',
    },
    // dictOptions: {
    //   type: Object,
    //   default: {},
    //   required: false,
    // },
  },
  computed: {
    val: {
      get() {
        return this.value
      },
      set() {
        this.$emit('input', this.value)
      },
    },
    queryParams() {
      const params = {
        ...this.params,
      }
      params.parentId = this.parentId
      return params
    },
  },
  watch: {
    parentId: {
      handler(id) {
        this.$set(this.params, 'parentId', id)
      },
			immediate: true
    },
  },
  methods: {
    // 获取详情
    getDetail(value) {
      const that = this;
      if (value instanceof Array) {
        this.$emit('select', {
          data: '',
          id: '',
        })
        return
      }

      let projectLeadingTemp = []
      let empIdArr = value.split(',')

      const parentId = this.parentId || '';
      erpPurchaseCategoryChildList({
        parentId
      }).then((res) => {
        if (res.success) {
          projectLeadingTemp = res.result.filter((item) => {
            return empIdArr.includes(item.id)
          })
          const title = projectLeadingTemp.map(item => item.categoryName).join(',')
          this.$emit('select', {
            data: title,
            id: value,
          })
          this.$nextTick(() => {
            this.$refs.hSelectInputPopup.title = title
          })
        }
      })
      // empIdArr.map((item, ind) => {
      //   if (item) {
      //     that.$refs.hSelectInputPopup.entities.forEach(it => {
      //       if(item === it.id) {
      //         projectLeadingTemp.push(it.categoryName)
      //       }
      //     })
      //     // projectLeadingTemp.push(filterMultiDictText(this.dictOptions['empId'], item + ''))
      //   }
      // })
      // const title = projectLeadingTemp.join(',')
      // this.$emit('select', {
      //   data: title,
      //   id: value,
      // })
      // this.$nextTick(() => {
      //   this.$refs.hSelectInputPopup.title = title
      // })
    },
  },
}
</script>

<style lang="scss" scoped>
// 按钮
.btn {
  position: fixed;
  bottom: 20rpx;
  width: 95%;
}
</style>
